﻿using Abp.AutoMapper;
using GN.Pay.Core.Entities;
using GN.Pay.Core.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core.Values.TradeTransfers
{
    /// <summary>
    /// 交易申请基础
    /// </summary>
    [AutoMap(typeof(TradeTransfersApply), typeof(TradeTransfersBook))]
    public class TradeTransfersApplyBase<TValueObject> : AValueObject<TValueObject>
        where TValueObject : TradeTransfersApplyBase<TValueObject>
    {
        /// <summary>
        /// 应用Id   
        /// </summary>
        [StringLength(36)]
        public string AppId { get; set; }

        /// <summary>
        /// 申请单号
        /// </summary>
        [Required]
        [StringLength(maximumLength: 32)]
        public string ApplyNo { get; set; }

        /// <summary>
        /// 申请金额
        /// </summary>
        [Range(0.01, (double)decimal.MaxValue, ErrorMessage = "申请金额必须大于零")]
        public decimal ApplyMoney { get; set; }       

        /// <summary>
        /// 账号
        /// </summary>
        [Required]
        [StringLength(maximumLength: 255)]
        public string AccountNo { get; set; }

        /// <summary>
        /// 收款人户名或真实姓名
        /// </summary>
        [Required]
        [StringLength(maximumLength: 255)]
        public string AccountName { get; set; }

        /// <summary>
        /// 收款人银行代码
        /// </summary>
        public string BankNo { get; set; }

        /// <summary>
        /// 开户行名称
        /// </summary>
        public string OpenBankName { get; set; }

        /// <summary>
        /// 省市
        /// </summary>
        public string ProvinceCity { get; set; }

        /// <summary>
        /// 交易通道Id
        /// </summary>
        [Required]
        public string ChannelId { get; set; }

        /// <summary>
        /// 应用 Ip
        /// </summary>
        public string AppIp { get; set; }

        /// <summary>
        /// 客户端Ip
        /// </summary>
        public string ClientIp { get; set; }

        /// <summary>
        /// 摘要
        /// </summary>
        [StringLength(maximumLength: 255)]
        public string Summary { get; set; }

        public override bool Equals(TValueObject other)
        {
            if (other == null)
            {
                return false;
            }
            return string.Compare(other.AppId, this.AppId, false) == 0
                && string.Compare(other.ApplyNo, this.ApplyNo, false) == 0;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Validate()
        {
            ApplyMoney = Math.Round(ApplyMoney, 2);
            if (ApplyMoney <= 0)
            {
                PayExceptionUtils.ThrowPayValidationException("申请金客必须大于零。");
            }
        }
    }
}
